package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.document.ExcelDataCsvDTO;
import com.andy.blog.admin.dto.document.ExcelDataDTO;
import com.andy.blog.admin.service.ExcelBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.utils.EasyExcelUtils;
import io.vavr.Tuple2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by andy on 2023-04-29 11:35
 */
@RestController
@RequestMapping("/document/excel")
public class ExcelController {

    @Resource
    private ExcelBusService excelBusService;

    /**
     * 导出csv
     */
    @Log(title = "导出csv", businessType = BusinessTypeEnum.EXPORT)
    //@PreAuthorize("@ss.hasPermi('system:dict:export')")
    @PostMapping("/exportCsv")
    public void exportCsv(HttpServletResponse response) {
        Tuple2<List<List<String>>, List<ExcelDataCsvDTO>> listTuple2 = excelBusService.exportCsv();
        EasyExcelUtils.writeCsvByHead(response, listTuple2._2, "csv", listTuple2._1);
    }

    /**
     * exportExcel
     */
    @Log(title = "导出excel", businessType = BusinessTypeEnum.EXPORT)
    //@PreAuthorize("@ss.hasPermi('system:dict:export')")
    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        List<ExcelDataDTO> excelDataDTOS = excelBusService.exportExcel();
        EasyExcelUtils.writeStyleExcel(response, excelDataDTOS, "excel", ExcelDataDTO.class);
    }


}
